package com.sllp.C71S3TsllpShop.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sllp.C71S3TsllpShop.bean.Address;
import com.sllp.C71S3TsllpShop.bean.AddressExample;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.AddressBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.UserBizImpl;
import com.sllp.C71S3TsllpShop.bean.AddressExample.Criteria;
import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.dao.AddressMapper;

@Controller
@SessionAttributes({"page","loginUser"})
public class AddressAction {
	
	@Resource
	private AddressMapper am;
	
	@Resource
	private UserBizImpl ubiz;
	@Resource
	private CarBizImpl cbiz;
	
	@Resource
	private AddressBizImpl abiz;
	
	@GetMapping("Member_Address.do")
	public String Addesrr(HttpSession session,@SessionAttribute("btlist") List<List<BigType>> lists,
			Model m,String pre,String next,
			@SessionAttribute(name="page",required=false)Integer page,
			@SessionAttribute(name="loginUser",required=false) User user) {
		m.addAttribute("btlist", lists);
		m.addAttribute("loginUser", user);
		if(next==null){
			page=1;
		}else if(Integer.valueOf(next)>0){
			page=Integer.valueOf(next);
		}
		if(pre!=null){
			page=1;
		}
		PageHelper.startPage(page,4);
		//从新设置分页的页码，相当于page++
		System.out.println(page+"========");
	    m.addAttribute("page", page);
	    m.addAttribute("pag", page-1);
	    
		AddressExample ae =new AddressExample();
		Criteria c= ae.createCriteria();
		c.andUserEqualTo(user.getId()).andStateEqualTo(2);
		List<Address>address = am.selectByExample(ae);
		m.addAttribute("addr", address);
		return "Member_Address";
	}
	
	/**
	 * 删除地址
	 */
	@ResponseBody
	@PostMapping("delAddress")
	public Result del(Model m,Integer id,@SessionAttribute(name="page")int page,
			@SessionAttribute(name="loginUser",required=false) User user){
		Address addr=am.selectByPrimaryKey(id);
		
		AddressExample ae =new AddressExample();
		Criteria c= ae.createCriteria();
		c.andUserEqualTo(user.getId()).andStateEqualTo(2);
		PageHelper.startPage(page,4);
		List<Address> address = am.selectByExample(ae);
		PageInfo<Address> pageInfo = new PageInfo<Address>(address);
		
		Address address2 = new Address();
		address2.setId(id);
		address2.setState(1);
		int ok = am.updateByPrimaryKeySelective(address2); // 删除不是删除，删除是修改状态 state
		
		/*
		 * count=true, pageNum=2, pageSize=4, startRow=4, endRow=8,
		 *  total=2, pages=1, reasonable=false, pageSizeZero=false
		 */
		// 判断这一页是不是第一页
		if(pageInfo.getPageNum()==1){
			page=1;
		}else if(addr.getId()==pageInfo.getList().get(0).getId() && pageInfo.getList().size()==1){
			// 判断是不是当前页的第一个,并且当前页只有一个
			page=page-1;
			System.out.println("=====");
		}
	    m.addAttribute("page", page);
		if(ok>0) {
			return new Result(ok,"删除成功",page);
		}
		return new Result(ok, page); 
	}
	/**
	 * 将地址设为默认地址
	 */
	@ResponseBody
	@PostMapping("update")
	public Result update(Integer id,@SessionAttribute(name="loginUser",required=false) User user,Model m){
		int ok=ubiz.updateAddress(id,user);
		user.setAddr(id);
		m.addAttribute("loginUser", user);
		if(ok>0) {
			return new Result(ok,"修改成功");
		}
		return new Result(ok, "修改失败"); 
	}

	/**
	 * 添加新地址和修改地址
	 */
	@ResponseBody
	@PostMapping("insertAdr")
	public Result insert(Address address,@SessionAttribute(name="loginUser",required=false) User user){
		address.setUser(user.getId());
		address.setState(2);
		int ok=0;
		if(address.getId()!=null && !address.getName().equals("")){
			Address a=am.selectByPrimaryKey(address.getId());
			if(a!=null){
				ok=am.updateByPrimaryKey(address);
			}
		}else{
		 	ok=am.insert(address);
		}
		 if(ok>0) {
			return new Result(ok,"保存成功");
		}
		return new Result(ok, "添加失败");
	}

	@ModelAttribute
	public void searchCar(@SessionAttribute("loginUser") User user,Model m) {
		if(user != null) {
			List<Car> clist=cbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
	}
}
